热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

COMFunctionsinPHP4(Windows)<续>

UsingthePHP4COMfunctionswithMSExcelAsfortheWordexampleabove,studythecodewiththehelpfromtheVisualBasicEditorObjectBrowserforExcel.?php#Settheworkbooktouseanditss
Using the PHP4 COM functions with MS Excel
As for the Word example above, study the code with the help from the Visual Basic Editor ObjectBrowser for Excel.

#Set the workbook to use and its sheet. In this example we use a spreadsheet that
#comes with the Excel installation called: SOLVSAMP.XLS

$workbook = "C:\Program Files\Microsoft office\Office\Samples\SOLVSAMP.XLS";
$sheet = "Quick Tour";

#Instantiate the spreadsheet component.
$ex = new COM("Excel.sheet") or Die ("Did not connect");

#Get the application name and version
print "Application name:{$ex->Application->value}
" ;
print "Loaded version: {$ex->Application->version}
";

#Open the workbook that we want to use.
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");

#Create a copy of the workbook, so the original workbook will be preserved.
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#$ex->Application->Visible = 1; #Uncomment to make Excel visible.

# Read and write to a cell in the new sheet
# We want to read the cell E11 (Advertising in the 4th. Quarter)
$sheets = $wkb->Worksheets($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->Cells(11,5) ; #Select the cell (Row Column number)
$cell->activate; #Activate the cell
print "Old Value = {$cell->value}
"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
print "New value = {$cell->value}
";#Print the new value=15000

#Eventually, recalculate the sheet with the new value.
$sheets->Calculate; #Necessary only if calc. option is manual
#And see the effect on total cost(Cell E13)
$cell = $sheets->Cells(13,5) ; #Select the cell (Row Column number)
$number = Number_format($cell->value);
print "New Total cost =\$$number - was \$47,732 before.
";
#Should print $57,809 because the advertising affects the Corporate overhead in the
# cell formula.

#Example of use of the built-in functions in Excel:
#Function: PMT(percent/12 months,Number of payments,Loan amount)
$pay = $ex->application->pmt(0.08/12,10,10000);
$pay = sprintf("%.2f",$pay);
print "Monthly payment for $10,000 loan @8% interest /10 months: \$ $pay
";
#Should print monthly payment = $ -1,037.03

#Optionally, save the modified workbook
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#Close all workbooks without questioning
$ex->application->ActiveWorkbook->Close("False");
unset ($ex);

?>
This example should get you going with the Excel COM and PHP. Of course there are many more objects to use. Writing an OOP wrapper for the principal functions will make access to the excel objects even easier.
Using PHP COM with Adobe Distiller
This last example is for a non-MS program: If your program has produced a PostScript document, it may be interesting to transform it (Distill it) to a PDF document. Adobe has a program called Distiller with a windows version that can be instantiated, with the following code:

$pdf = new COM("pdfdistiller.pdfdistiller.1");

?>
Note that the OLE Identifier name is not obvious, especially when the distiller documentation (Adobe's Technical Note #5158) refers to it as "pdfdistiller."
The principal method to distill a document is:

$pdf->FileToPdf ($psfile, strOutputPDF '
', strJobOptions "");

?>
Where $psfile is the name of the PostScript file, strOutputPDF is the name for the output PDF file. StrJobOptions is the name of the parameters file for Distiller. The two last parameters of the method can be left blank to use the same name, the PS file for the PDF file and to use the default Job options file. For example:

$pdf->FileToPdf ($psfile, "", "");
#Where $psfile could be Myfile.ps and the result file: Myfile.pdf

?>
There are more methods and properties that can be used with Distiller. If you are interested, look at the Adobe'
s technical note.
Caveats/Possible problems
If there are some errors in your code, you may instantiate the object and your program may not close before it times out. Worst of all, the application may retentively be instantiated. As a result, several copies may lay around in your programs list and interfere after you have corrected the problem. The solution: After fixing the bug, clean up (<CTRL+ALT+Delete> and End Task) all the instances in the program list before you restart. For this same reason, always close the application at the end of your code and unlink the instance.
You may experience some oddities with com_get and com_set. For example: $Version = Com_get($instance->Application,"Version"); Works with Word, but produces an error with Excel.
Some Objects won't be instantiated by PHP4, it appears that these objects need a custom interface that PHP4-COM doesn't support.
Why use it?
Hopefully, these three examples have shown you the ropes. PHP COM allows the connection to many Windows programs inside a PHP script. The code is simpler than ASP's and can be integrated with the rest of PHP's powerful database functions. Microsoft markets the COM technology everywhere and under different names and architectures, like COM+(Combine COM with Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, etc. PHP and Apache, working together, are now offering an open source solution to this confusion.
http://phpclasses.upperdesign.com/browse.html?package=86
--Alain
PS: See the EXCEL class using the COM interface at:
推荐阅读
  • PHP 5.5.0rc1 发布:深入解析 Zend OPcache
    2013年5月9日,PHP官方发布了PHP 5.5.0rc1和PHP 5.4.15正式版,这两个版本均支持64位环境。本文将详细介绍Zend OPcache的功能及其在Windows环境下的配置与测试。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 在 Windows 10 中,F1 至 F12 键默认设置为快捷功能键。本文将介绍几种有效方法来禁用这些快捷键,并恢复其标准功能键的作用。请注意,部分笔记本电脑的快捷键可能无法完全关闭。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在哈佛大学商学院举行的Cyberposium大会上,专家们深入探讨了开源软件的崛起及其对企业市场的影响。会议指出,开源软件不仅为企业提供了新的增长机会,还促进了软件质量的提升和创新。 ... [详细]
author-avatar
mobiledu2502856483
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有